package params

import (
	"errors"
	"micro-oauth/datamodels/enum"
	"micro-oauth/utils"
	"regexp"
	"strings"
)

type TriggerVendorUserStatusRequest struct {
	UserId uint64          `json:"user_id,string" form:"user_id"`
	Status enum.UserStatus `json:"status" form:"status"`
}

type VendorUserListRequest struct {
	ListRequest
	ScopeId   uint64            `json:"scope_id" form:"scope_id"`
	Status    enum.UserStatus   `json:"name" form:"status"`
	Type      enum.UserType     `json:"type" form:"type"`
	VendorIds Uint64StringSlice `json:"vendor_ids" form:"vendor_ids"`
}

func (req *TriggerVendorUserStatusRequest) Validate() error {
	if req.UserId == 0 {
		return errors.New("user id can not be empty")
	}
	if req.Status != enum.UserStatus_Active && req.Status != enum.UserStatus_Forbidden {
		return errors.New("wrong user status(only can be ACTIVE or FORBIDDEN")
	}
	return nil
}

type VendorManagerCreateRequest struct {
	Name string `json:"name" form:"name"`
	PWD  string `json:"pwd" form:"pwd"`
}

func (req *VendorManagerCreateRequest) Validate() error {
	req.Name = strings.Trim(req.Name, " \t\n")
	req.PWD = strings.Trim(req.PWD, " \t\n")
	if req.Name == "" {
		return errors.New("name can not be empty")
	}
	if req.PWD == "" {
		return errors.New("pwd can not be empty")
	}
	err := utils.CheckStrPwdQualify(req.PWD)
	if err != nil {
		return err
	}
	return nil
}

type VendorUserCreateRequest struct {
	Email    string `json:"email" form:"vendor_id"`
	VendorId uint64 `json:"vendor_id,string" form:"vendor_id"`
}

func (req *VendorUserCreateRequest) Validate() error {
	req.Email = strings.Trim(req.Email, " \t\n")
	if !validateEmail(req.Email) {
		return errors.New("invalidate email")
	}
	if req.VendorId == 0 {
		return errors.New("vendor id can not be empty")
	}
	return nil
}

func validateEmail(email string) bool {
	emailRegexp := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
	return emailRegexp.MatchString(email)
}

type ResetVendorManagerUserUpdateRequest struct {
	Name   *string `json:"name" form:"name"`
	UserId uint64  `json:"user_id,string" form:"user_id"`
	NewPwd string  `json:"new_pwd" form:"new_pwd"`
	OldPwd string  `json:"old_pwd" form:"old_pwd"`
}

func (req *ResetVendorManagerUserUpdateRequest) Validate() error {
	req.NewPwd = strings.Trim(req.NewPwd, " \t\n")
	req.OldPwd = strings.Trim(req.OldPwd, " \t\n")
	if req.NewPwd == "" || req.OldPwd == "" {
		return errors.New("new_pwd or old_pwd can't be empty")
	}
	if err := utils.CheckStrPwdQualify(req.NewPwd); err != nil {
		return err
	}
	if req.UserId == 0 {
		return errors.New("user_id can not be zero")
	}
	return nil
}

type ResetVendorUserPwdRequest struct {
	UserId   uint64 `json:"user_id,string" form:"user_id"`
	NewEmail string `json:"new_email" form:"new_email"`
}

func (req *ResetVendorUserPwdRequest) Validate() error {
	req.NewEmail = strings.Trim(req.NewEmail, " \t\n")
	if req.NewEmail == "" {
		return errors.New("new_email can't be empty")
	}
	if !validateEmail(req.NewEmail) {
		return errors.New("new_email not validate")
	}
	if req.UserId == 0 {
		return errors.New("user_id can not be zero")
	}
	return nil
}
